﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs;

namespace Butcher.winform.EnterpriseForms.Fatoiles
{
    //uiFlowLayoutPanel1
    public partial class FatoilList : Form
    {
        string BulkSlaughterNo1 = "";
        public FatoilList()
        {
            InitializeComponent();

            string url = @"http://localhost:5229/api/Batch/BulkSlaughterSearch";
            HttpClient client = new HttpClient();
            BulkSlaughter ctr = new BulkSlaughter();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<BulkSlaughter>(ctr));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<BulkSlaughter> cts = aaa.ToObject<List<BulkSlaughter>>();
            UIFlowLayoutPanel[] appro = new UIFlowLayoutPanel[cts.Count];
            for (int t = 0; t < appro.Length; t++)
            {
                UIFlowLayoutPanel uiflow = new UIFlowLayoutPanel();
                uiflow.Size = new Size(180, 90);

                Button[] btn = new Button[appro.Length];
                btn[t] = new Button();
                btn[t].Size = new Size(100, 30);

                BulkSlaughter asas = cts[t];
                //转换为字符串
                btn[t].Name = Json.Serialize(asas.BulkSlaughterNo);
                //转换为实体
                BulkSlaughter babab = Json.Deserialize<BulkSlaughter>(btn[t].Name);
                btn[t].Text = "--获取--";

                for (int i = 0; i < 2; i++)
                {
                    Label newlabel = new Label();
                    Label newlabel2 = new Label();
                    newlabel.Width = 80;
                    newlabel2.Width = 80;
                    if (i == 0)
                    {
                        newlabel.Text = "屠宰计划:";
                        newlabel2.Text = cts[t].BulkSlaughterNo;
                    }
                    uiflow.Controls.Add(newlabel);
                    uiflow.Controls.Add(newlabel2);
                }
                btn[t].Click += new System.EventHandler(this.Button_Click);
                uiflow.Controls.Add(btn[t]);
                uiFlowLayoutPanel1.Controls.Add(uiflow);
            }


            textBox2.LostFocus += new EventHandler(textBox2_GotFocus);
            getList();
            getWeightList();
        }
        private void Button_Click(object sender, EventArgs e)
        {

            Button button = (Button)sender;
            BulkSlaughterNo1 = button.Name;
            MessageBox.Show(button.Name);
        }
        Scale ScaleInfo = new Scale();
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            ScaleBackfill.pid = 5;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Hide();
            scale.ShowDialog();
        }
        public void textBox2_GotFocus(object sender, EventArgs e)
        {
            textBox4.Text = (Convert.ToDecimal(textBox2.Text) - Convert.ToDecimal(textBox3.Text)).ToString();
        }
        public void getList()
        {
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                textBox3.Text = ScaleInfo.TareWeightDeduction.ToString();
                getStash(ScaleInfo.Warehouse);
            }
        }
        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            WeighTheFat weighTheFat = new WeighTheFat();
            weighTheFat.RepositoryID = Convert.ToInt32(cb_Level.SelectedValue);
            weighTheFat.BulkSlaughterNo = BulkSlaughterNo1;
            weighTheFat.WeighTime = DateTime.Now;
            weighTheFat.ProductCode = 1;
            weighTheFat.WeightNum = Convert.ToInt32(textBox5.Text);
            weighTheFat.Tare = Convert.ToDecimal(textBox3.Text);
            weighTheFat.NetWeight = Convert.ToDecimal(textBox4.Text);
            weighTheFat.AverageWeight = Convert.ToDecimal(textBox4.Text);
            weighTheFat.Remark = textBox7.Text;
            weighTheFat.ScaleId = ScaleInfo.Scaleid;
            string url = @"http://localhost:5229/api/Ending/WeighTheFatCreate";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<WeighTheFat>(weighTheFat));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            getWeightList();
        }
        public class Page
        {
            public int pageindex { get; set; }

            public int Pagesize { get; set; }
        }
        public void getWeightList()
        {
            Page pages = new Page();
            pages.pageindex = 1;
            pages.Pagesize = 5;
            string url = @"http://localhost:5229/api/Ending/HandleList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Page>(pages));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["lists"].Value<JArray>();
            List<WeighTheFat> enterprises = aaa.ToObject<List<WeighTheFat>>();
            dataGridView1.DataSource = enterprises;
        }
    }
}
